package by.bucha.testproject.adapters;

import java.util.List;

import by.bucha.testproject.R;
import by.bucha.testproject.content.bo.fileExplorer.FileExplorerListItemModel;
import by.bucha.testproject.fragments.FileExplorerFragment;
import by.bucha.testproject.imageLoader.ImageLoaderCallback;
import by.bucha.testproject.imageLoader.ImageLoaderData;
import by.bucha.testproject.utils.TaskApplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FileArrayAdapter extends ArrayAdapter<FileExplorerListItemModel> {

	private int mTextViewResourceId;
	private List<FileExplorerListItemModel> mItems;
	private int mPlaceholder = R.drawable.ic_stub_image;

	private int mFolderResurce = R.drawable.ic_directory_icon;

	private int mBackResurce = R.drawable.ic_directory_up;

	public FileArrayAdapter(Context context, int textViewResourceId,
			List<FileExplorerListItemModel> objects) {
		super(context, textViewResourceId, objects);
		mTextViewResourceId = textViewResourceId;
		mItems = objects;
	}

	@Override
	public FileExplorerListItemModel getItem(int i) {
		if (mItems != null) {
			return mItems.get(i);
		} else {
			return null;
		}
	}

	@Override
	public int getCount() {
		if (mItems != null) {
			return mItems.size();
		} else {
			return 0;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(mTextViewResourceId, null);
		}

		final FileExplorerListItemModel model = mItems.get(position);
		if (model != null) {
			TextView nameTextView = (TextView) view
					.findViewById(R.id.view_fragment_file_explorer_name);
			TextView sizeTextView = (TextView) view
					.findViewById(R.id.view_fragment_file_explorer_size);
			TextView dateTextView = (TextView) view
					.findViewById(R.id.view_fragment_file_explorer_date);

			final ImageView imageView = (ImageView) view
					.findViewById(R.id.view_fragment_file_explorer_image);

			if (model != null && model.isFolder()) {
				imageView.setImageResource(mFolderResurce);
			} else if (model != null && model.isBack()) {
				imageView.setImageResource(mBackResurce);
			} else if (model != null && model.isImage()) {
				if (!FileExplorerFragment.sBusy) {
					int height = imageView.getLayoutParams().height;
					int width = imageView.getLayoutParams().width;
					TaskApplication.getImageLoader().bind(
							new ImageLoaderData(imageView, model.getPath(),
									width, height), new ImageLoaderCallback() {

								@Override
								public void onSucsess() {
									imageView.postInvalidate();
								}

								@Override
								public void onError(Exception e) {
									String warning = getContext()
											.getResources()
											.getString(
													R.string.file_explorer_fragment_strings_no_data);

									Toast.makeText(getContext(),
											warning + " : " + e.getMessage(),
											Toast.LENGTH_SHORT).show();

								}
							});
				} else {
					imageView.setImageResource(mPlaceholder);
					convertView.setTag(this);
				}
			}

			nameTextView.setText(model.getName());

			sizeTextView.setText(model.getData());

			dateTextView.setText(model.getDate());

		}
		return view;
	}
}
